//
//  GXDailyBigAlbumTableViewCell.m
//  GxLanOuProject
//
//  Created by 甘霞 on 15/10/28.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXDailyBigAlbumTableViewCell.h"
#import "DailyViewCellHeader.h"
#import "GXCircleImageViewAndLabel.h"
#import "NSString+GXSizeWithText.h"
#import "DailyViewCellHeader.h"
#import "GXSuggestModel.h"
//#import "FontSizeManager.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"

@interface GXDailyBigAlbumTableViewCell ()

@property (nonatomic, retain) UIImageView *mainImageView;
@property (nonatomic, retain) UIImageView *authorImageView;
@property (nonatomic, retain) UILabel *authorLabel;
@property (nonatomic, retain) UIImageView *qouteImageView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *introLabel;
@property (nonatomic, retain) UILabel *descLabel;
@property (nonatomic, retain) GXCircleImageViewAndLabel *circleImage;

@end

@implementation GXDailyBigAlbumTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {
    
    BOOL isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
    self.backgroundColor = isNight ? [UIColor blackColor] : [UIColor whiteColor];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeModel) name:@"chanegModel" object:nil];
    
    //大图
    self.mainImageView = [[UIImageView alloc] init];
    [self.contentView  addSubview:self.mainImageView];

    //标题
    self.titleLabel = [[UILabel alloc] init];
    [self.mainImageView addSubview:self.titleLabel];
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    self.titleLabel.textColor = isNight ? kNightColor_SuggestTitle : kDayColor_SuggestTitle;
    [self.titleLabel setNumberOfLines:0];
    [self.titleLabel setFont:[UIFont systemFontOfSize:kTitleLabelFontSize]];
    
    //引号
    self.qouteImageView = [[UIImageView alloc] init];
    self.qouteImageView.image = isNight ? [UIImage imageNamed:@"icon-quote-white"] : [UIImage imageNamed:@"icon-quote-white"];
    [self.mainImageView addSubview:self.qouteImageView];
    
    //简介
    self.introLabel = [[UILabel alloc] init];
    self.introLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
    [self.introLabel setFont:[UIFont systemFontOfSize:kIntroLabelFontSize]];
    [self.introLabel setNumberOfLines:0];
    [self.introLabel setTextAlignment:NSTextAlignmentCenter];
    [self.mainImageView addSubview:self.introLabel];
    
    //描述
    self.descLabel = [[UILabel alloc] init];
    self.descLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
    [self.descLabel setFont:[UIFont systemFontOfSize:kSubTitleLabelFontSize]];
    [self.descLabel setTextAlignment:NSTextAlignmentCenter];
    [self.descLabel setNumberOfLines:0];
    [self.mainImageView addSubview:self.descLabel];
    
    //作者投降
    self.authorImageView = [[UIImageView alloc] init];
    [self.mainImageView addSubview:self.authorImageView];
    [self.authorImageView.layer setCornerRadius:15];
    self.authorImageView.layer.masksToBounds = YES;
    
    //作者名字
    self.authorLabel = [[UILabel alloc] init];
    [self.mainImageView addSubview:self.authorLabel];
    [self.authorLabel setFont:[UIFont systemFontOfSize:kSubTitleLabelFontSize]];
    [self.authorLabel setTextColor:[UIColor whiteColor]];
    
    //显示图片数量
    self.circleImage = [[GXCircleImageViewAndLabel alloc] init];
    [self.contentView addSubview:self.circleImage];
}

- (void)changeModel {
    BOOL isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
    self.backgroundColor = isNight ? [UIColor blackColor] : [UIColor whiteColor];
    self.titleLabel.textColor = isNight ? kNightColor_SuggestTitle : kDayColor_SuggestTitle;
    self.qouteImageView.image = isNight ? [UIImage imageNamed:@"icon-quote-white"] : [UIImage imageNamed:@"icon-quote-white"];
    self.introLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
    self.descLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //图片
    [self.mainImageView setFrame:CGRectMake(0, kStringBorder, kCellWidth, kMainImageViewHeight)];
    //作者头像
    [self.authorImageView setFrame:CGRectMake(kSpecialCommentBorder, kSpecialCommentBorder, kAuthorImageViewSize, kAuthorImageViewSize)];
    [self.authorImageView setBackgroundColor:[UIColor blackColor]];
    //作者名字
    CGFloat authorLabelX = CGRectGetMaxX(self.authorImageView.frame) + 10;
    [self.authorLabel setFrame:CGRectMake(authorLabelX, kSpecialCommentBorder, kCellWidth - 2 * kSpecialCommentBorder, kTimeLabelHeight)];
    //引号
    CGSize qouteImageSize = self.qouteImageView.image.size;
    [self.qouteImageView setFrame:CGRectMake(kCellWidth / 2.0 - qouteImageSize.width / 2.0, kQouteTopBorder, qouteImageSize.width, qouteImageSize.height)];
    CGFloat labelW = kCellWidth - 2 * kSpecialCommentBorder;
    //简介
    CGFloat introLabelY = CGRectGetMaxY(self.qouteImageView.frame);
    CGRect introLabelSize = [self.introLabel.text SizeWithTextFontSize:kIntroLabelFontSize textWidth:labelW];
    [self.introLabel setFrame:CGRectMake(kSpecialCommentBorder, introLabelY, labelW, introLabelSize.size.height)];
    //标题
    CGFloat titleLabelY = CGRectGetMaxY(self.introLabel.frame) + kSpecialCommentBorder;
    CGRect titleLabelSize = [self.titleLabel.text SizeWithTextFontSize:kTitleLabelFontSize textWidth:labelW];
    [self.titleLabel setFrame:CGRectMake(kSpecialCommentBorder, titleLabelY, labelW, titleLabelSize.size.height)];
    //描述
    CGFloat descLabelY = CGRectGetMaxY(self.titleLabel.frame) + kSpecialCommentBorder
    ;
    CGRect descSize = [self.descLabel.text SizeWithTextFontSize:kSubTitleLabelFontSize textWidth:labelW];
    [self.descLabel setFrame:CGRectMake(kSpecialCommentBorder, descLabelY, labelW, descSize.size.height)];
    
    CGFloat btnY = self.contentView.frame.size.height - kSpecialBtnSize - kSpecialCommentBorder;
    //显示图片数量
    [self.circleImage setFrame:CGRectMake(kCellWidth - 60 - kSpecialCommentBorder, btnY - 5, 60, kSpecialBtnSize)];
}

- (void)setSuggestModel:(GXSuggestModel *)suggestModel {
    if (_suggestModel != suggestModel) {
        _suggestModel = suggestModel;
    }
    
    //图片
    [self.mainImageView sd_setImageWithURL:_suggestModel.thumb[@"raw"] placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    //作者投降
    [self.authorImageView sd_setImageWithURL:_suggestModel.cp[@"avatar"]];
    //作者名字
    [self.authorLabel setText:_suggestModel.cp[@"screen_name"]];
    //简介
    [self.introLabel setText:_suggestModel.intro];
    //标题
    [self.titleLabel setText:_suggestModel.title];
    //描述
    [self.descLabel setText:_suggestModel.content[@"desc"]];
    //图片数量
    [self.circleImage.label setText:[NSString stringWithFormat:@"%@张", _suggestModel.gallery[@"image_count"]]];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
